<?php

/**
 * @Author: king
 * @Date:   2019-06-22 15:48:48
 * @Last Modified by:   king
 * @Last Modified time: 2019-06-22 16:03:18
 */
namespace App\Http\Controllers\Api\v3;

use App\Http\Controllers\Api\BaseController;
use App\Tools\Chart;
use App\Models\Api\Recode;
use App\Models\Api\Orders;
use App\Models\Api\AwardUser;

use App\Tools\Astrolog\Astrolog;
use App\Http\Controllers\Api\v1\Everyday\IndexController as Day;
use Illuminate\Http\Request;
use Cache;

class ZiweiController extends BaseController
{
    public $nowData = [];
    public function index(Request $request)
    {
         return view('web/ziwei.ziwei');

    }
    public function chart(Request $request)
    {
        if (!$request->isMethod('post')) {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $param    = $request->all();
        $chart    = new Chart();
        $birthday = $param['birthday'];
        $hour     = $param['hour'];
        $gender   = $param['gender'];
        $result   = $chart->run($birthday, $hour, $gender);
        return show(config('code.success'), '加载成功', $result);

    }

 public function getNow(Request $request)
{
     $device = $request->header();
     print_r($device);
     die;
    // $recodeModel = new Recode();
    // $recode_info[] = $recodeModel->getRecode(['id'=>'553464']);
    //   $start_time = date('Y','1704038400');
    //                   $end_time = date('Y',strtotime('+1years','1704038400' - 86400*365));
    //                   $order_time  = prDates([
    //                       'start' => $start_time,
    //                       'end' => $end_time,
    //                       'type' => 3,
    //                   ]);
    //                   //var_dump($order_time);die;
    //   (new Orders())->where('id','1483')->where(['status'=>'4'])->update(['status'=>1]);
    //             //将运势订单加入队列,重新计算
    //             $order_push=[
    //                               'order_id' => '02202312231331582361',
    //                               'oid'=>1483,
    //                               'recode_info' => $recode_info,
    //                               'unlock_time' => $order_time,
    //                               'ostatus' => 1,
    //                               'ftype' => 3,
    //                               'is_last'=>0
    //                           ];
    //             $arr[]=$order_push;
    //             $count = count($arr);
    //             foreach ($arr as $k =>$value)
    //             {
    //                 if ($k+1 == $count)
    //                 {
    //                     $value['is_last'] =1;
    //                 }
    //                 fortune_redis_job($value);
    //             }
    //     die;

    //  die;
    $recodeModel = new Recode();
    $recode_info[] = $recodeModel->getRecode(['id'=>'553464']);
      $start_time = date('Y-m-d','1704070800');
                       $end_time = date('Y-m-d',strtotime('+1months','1704070800'));
                      // var_dump($end_time);die;
                       $order_time  = prDates([
                           'start' => $start_time,
                           'end' => $end_time,
                           'type' => 2,
                       ]);
                      //var_dump($order_time);die;
      (new Orders())->where('id','1492')->where(['status'=>'4'])->update(['status'=>1]);
                //将运势订单加入队列,重新计算
                $order_push=[
                                   'order_id' => '02202312231333104895',
                                   'oid'=>1492,
                                   'recode_info' => $recode_info,
                                   'unlock_time' => $order_time,
                                   'ostatus' => 1,
                                   'ftype' => 2,
                                   'is_last'=>0
                               ];
                $arr[]=$order_push;
                $count = count($arr);
                foreach ($arr as $k =>$value)
                {
                    if ($k+1 == $count)
                    {
                        $value['is_last'] =1;
                    }
                    fortune_redis_job($value);
                }
        die;
       $time_now    = $request->get('time');
       $map = ['uid' => $request->uid, 'isself' => 1];
       $recode_info = (new Recode())->getSelfRecode($map);
       $maps = ['id' => '553891', 'isself' => 1];
       $recodeinfo = (new Recode())->getRecode($maps);
      // var_dump($recodeinfo);die;

         if (empty($recode_info)) {
            return show(config('code.denied'), '用户信息获取失败,请重新登录', [], 403);
        }


        $recode_info['longitude'] = ($recode_info['long_deg'] + $recode_info['long_min'] / 60 ) * $recode_info['ew'];

        $recode_info['latitude'] = ($recode_info['lat_deg'] + $recode_info['lat_min'] / 60 ) * $recode_info['ns'];
        $dailyTime = $time_now;
        //var_dump($dailyTime);die;
        $timeinfo = [
                'year' => date('Y', $dailyTime),
                'month' => date('m', $dailyTime),
                'day' => date('d', $dailyTime),
                'hour' => date('H', $dailyTime),
                'minute' => date('i', $dailyTime),
            ];
            $timeinfo['timezone'] = 8;
            $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'], $timeinfo['minute'], 0,
            $timeinfo['month'], $timeinfo['day'], $timeinfo['year']) ? 1 : 0;
            $dailyFortune = new Day();
             $dailyFortune->astro = Astrolog::getInstance();
             $dailyFortune->astro
                ->setRecode1($recode_info)
                ->setAdvance($timeinfo)
                ->setExecPrefix(config('astrolog.prefix'))
                ->setIsAstrolabe(false)
                ->setUserSetting(config('astroset.setting1'))
                ->setGetDatas(['now', 'natal', 'transits', 'thirdprogressed'])
                ->getAstroDatas();
             // 获取今日天象
            //$timeinfo['hour'] = '00';
            $dailyFortune->nowData = $dailyFortune->astro
                ->setAdvance($timeinfo)
                ->setGetDatas(['now'])
                ->getAstroDatas()
                ->getDatas('now');

            $tips = $dailyFortune->getAstrologicalDetailst($time_now);
            var_dump($tips);

   }

    public function award(Request $request){
        $rid = $request->input('rid');
        $uid = $request->uid;
        $type = $request->input('type','3');
        $AwardModel = new AwardUser();
        //获取当前档案的信息
        $recodeModel = new Recode();
        $recode_info[] = $recodeModel->getRecode(['id'=>$rid]);
        //获取当前用户自己的档案信息
        $self_recode= $recodeModel->getSelfRecode(['uid'=>$uid]);
        $arr  =[];
        $award_arr  =[];
        $is_award = 0;
              if ($rid == $self_recode['id'])
                      {
                          $award = $AwardModel->get_award_fortune(['uid'=>$uid,'rid'=>$rid,'type'=>4]);
                          $award = $award ? $award : [];
                          if ($award)
                          {
                              foreach ($award as $item)
                              {
                                  //获取一笔赠送的时间范围
                                  $award_time = json_decode($item['extend'],true)['award_time'];
                                  $start_time = date('Y',$award_time);
                                  $end_time = date('Y',strtotime($item['expire_time'],$award_time));
                                  $order_time  = prDates([
                                      'start' => $start_time,
                                      'end' => $end_time,
                                      'type' => $type,
                                  ]);
                                 //var_dump($order_time);die;
                                  $award_push=[];
                                  //若解锁的时间不在最新版本的时间内,重新计算加入队列
                                  foreach ($order_time as $vv)
                                  {

                                          //获取加入队列的赠送id
                                          $aids[]=$item['id'];
                                          //获取加入队列的数据
                                          $award_push=[
                                              'aid' => $item['aid'],
                                              'recode_info' => $recode_info,
                                              'unlock_time' => $order_time,
                                              'ftype' => 4,
                                              'type'=>'year',
                                              'is_last'=>0
                                          ];

                                          $is_award = 1;
                                          //删除重新计算的赠送缓存
                                          Cache::forget($uid.'_'.$rid.'_award_fortune_' . 4);

                                  }
                                  //将多笔未升级的赠送添加队列,重新计算
                                  if ($award_push)
                                  {
                                      $award_arr[]=$award_push;
                                  }
                              }
                          }
                      }

         if ($rid == $self_recode['id'] && $aids  && $award_arr && $is_award ==1)
             {
                $AwardModel->where(['status'=>1])->whereIn('id',$aids)->update(['status'=>0]);

                 //将运势中奖记录加入订单重新计算
                 $count = count($award_arr);
                 foreach ($award_arr as $k=>$item)
                 {
                     // 运势队列消费时需要的数据
                     $jobData=[
                         'aid' => $item['aid'],
                         'recode' => $recode_info,
                         'start_time' => $item['unlock_time'],
                         'ftype' => $item['ftype'],
                         'type'=> $item['type'],
                         'is_last'=>0
                     ];

                     if ($k+1 == $count)
                     {
                         $arr[$k]['is_last'] =1;
                     }

                     $queueConfig = config('queue');

                     if(getVip($recode_info[0]['uid']) >= 1)
                     {
                         $queueConfig['fortune_delay'] = 0;
                     }
                     queuePush($queueConfig['fortune_task'], $jobData, $queueConfig['fortune_queue'],$queueConfig['fortune_delay']);
//                     rabbitTask('fortune',$jobData);
                 }
             }
    }

public function award2(Request $request){
        $rid = $request->input('rid');
        $uid = $request->uid;
        $type = $request->input('type','2');
        $AwardModel = new AwardUser();
        //获取当前档案的信息
        $recodeModel = new Recode();
        $recode_info[] = $recodeModel->getRecode(['id'=>$rid]);
        //获取当前用户自己的档案信息
        $self_recode= $recodeModel->getSelfRecode(['uid'=>$uid]);
        $arr  =[];
        $award_arr  =[];
        $is_award = 0;
              if ($rid == $self_recode['id'])
                      {
                          //获取当前档案的赠送记录
                  $award = $AwardModel->get_award_fortune(['uid'=>$uid,'rid'=>$rid,'type'=>3]);
                  $award = $award ? $award : [];

                  if ($award)
                  {
                      foreach ($award as $item)
                      {
                          //获取一笔赠送的时间范围
                          $award_time = json_decode($item['extend'],true)['award_time'];
                          $start_time = date('Y-m',$award_time);
                          $end_time = date('Y-m',strtotime($item['expire_time'],$award_time));
                          $order_time  = prDates([
                              'start' => $start_time,
                              'end' => $end_time,
                              'type' => $type,
                          ]);

                          $award_push=[];
                          //若解锁的时间不在最新版本的时间内,重新计算加入队列
                          foreach ($order_time as $vv)
                          {

                                  //获取重新计算的赠送id
                                  $aids[]=$item['id'];
                                  //获取加入队列的数据
                                  $award_push=[
                                      'aid' => $item['aid'],
                                      'recode_info' => $recode_info,
                                      'unlock_time' => $order_time,
                                      'ftype' => 3,
                                      'type'=>'month',
                                      'is_last'=>0
                                  ];
                                  $is_award = 1;
                                  //删除重新计算的赠送缓存
                                  Cache::forget($uid.'_'.$rid.'_award_fortune_' . 3);
                              }
                                  //将多笔未升级的赠送添加队列,重新计算
                                  if ($award_push)
                                  {
                                      $award_arr[]=$award_push;
                                  }
                              }
                          }
                      }

         if ($rid == $self_recode['id'] && $aids  && $award_arr && $is_award ==1)
             {
                $AwardModel->where(['status'=>1])->whereIn('id',$aids)->update(['status'=>0]);

                 //将运势中奖记录加入订单重新计算
                 $count = count($award_arr);
                 foreach ($award_arr as $k=>$item)
                 {
                     // 运势队列消费时需要的数据
                     $jobData=[
                         'aid' => $item['aid'],
                         'recode' => $recode_info,
                         'start_time' => $item['unlock_time'],
                         'ftype' => $item['ftype'],
                         'type'=> $item['type'],
                         'is_last'=>0
                     ];

                     if ($k+1 == $count)
                     {
                         $arr[$k]['is_last'] =1;
                     }

                     $queueConfig = config('queue');

                     if(getVip($recode_info[0]['uid']) >= 1)
                     {
                         $queueConfig['fortune_delay'] = 0;
                     }
                     queuePush($queueConfig['fortune_task'], $jobData, $queueConfig['fortune_queue'],$queueConfig['fortune_delay']);
//                     rabbitTask('fortune',$jobData);
                 }
             }
    }


}
